package com.abc.recycling.repository;

import com.abc.recycling.model.RecyclingStation;
import org.springframework.data.domain.Page;
import org.springframework.data.domain.Pageable;
import org.springframework.data.jpa.repository.JpaRepository;
import org.springframework.data.jpa.repository.Query;
import org.springframework.data.repository.query.Param;
import org.springframework.stereotype.Repository;

import java.util.List;

@Repository
public interface RecyclingStationRepository extends JpaRepository<RecyclingStation, Long> {

    /**
     * 根据条件查询回收站
     */
    @Query("SELECT s FROM RecyclingStation s WHERE " +
           "(:name IS NULL OR s.name LIKE %:name%) AND " +
           "(:address IS NULL OR s.address LIKE %:address%) AND " +
           "(:status IS NULL OR s.status = :status)")
    List<RecyclingStation> findByConditions(
            @Param("name") String name,
            @Param("address") String address,
            @Param("status") Integer status);

    List<RecyclingStation> findByNameContainingOrAddressContaining(String name, String address);
    
    Page<RecyclingStation> findByNameContainingOrAddressContaining(String name, String address, Pageable pageable);
    
    Page<RecyclingStation> findByAddressContaining(String address, Pageable pageable);

    /**
     * 查找附近的回收站
     */
    @Query("SELECT s FROM RecyclingStation s WHERE " +
           "s.latitude BETWEEN :minLat AND :maxLat AND " +
           "s.longitude BETWEEN :minLon AND :maxLon")
    List<RecyclingStation> findNearbyStations(
            @Param("minLat") double minLat,
            @Param("maxLat") double maxLat,
            @Param("minLon") double minLon,
            @Param("maxLon") double maxLon);
} 